/*  ------------------------------------------------------------------------  */
/*  Paper        : Does Stakeholder Outrage Determine Executive Pay?          */
/*  Program      : a01-04-crsp.do                                             */
/*  Description  : CRSP variables                                             */
/*  ------------------------------------------------------------------------  */

/*  ------------------------------------------------------------------------  */
//  Import CRSP / Compustat linking history

import sas using "$crsp/a_ccm/comphist.sas7bdat", clear
	br
	rename *, lower
	keep if !missing(hcik)
	keep gvkey hsic hchgdt hchgenddt
	format hchgdt hchgenddt %tdCCYY-NN-DD
	replace hchgenddt=date("20251231","YMD") if missing(hchgenddt)
save "$temp112/comphist.dta", replace

/*  ------------------------------------------------------------------------  */
//  Import CRSP / Compustat linking history

import sas using "$crsp/a_ccm/ccmxpf_lnkhist.sas7bdat", clear
	br
	rename *, lower
	format linkdt linkenddt %tdCCYY-NN-DD
	replace linkenddt=date("20251231","YMD") if missing(linkenddt)
	keep if !missing(lpermno)
	tab linktype
	keep if inlist(linktype,"LC","LU")
	xtset lpermno linkdt
save "$temp112/ccmxpf_lnkhist.dta", replace

/*  ------------------------------------------------------------------------  */
//  Import CRSP Stock header

import sas using "$crsp/m_stock/dsfhdr.sas7bdat", clear
	br
	rename *, lower
	keep permno begdat enddat
	format begdat enddat %tdCCYY-NN-DD
save "$temp112/dsfhdr.dta", replace

/*  ------------------------------------------------------------------------  */
//  Preparing SAS datasets for merging

import sas using "$ff/factors_monthly.sas7bdat", clear
	br
	rename *, lower
	format date dateff %tdCCYY-NN-DD
	gen mkt = (mktrf+rf)
	keep date dateff mkt mktrf rf
	gen date_fd = dofm(mofd(date))
	format date_fd %tdCCYY-NN-DD
	drop date dateff
save "$temp112/factors_monthly.dta", replace

import sas using "$crsp/m_stock/msedelist.sas7bdat", clear
	br
	rename *, lower
	gen date_fd = dofm(mofd(dlstdt))
	format date_fd dlstdt %tdCCYY-NN-DD
	keep permno date_fd dlstdt dlret
save "$temp112/msedelist.dta", replace

/*  ------------------------------------------------------------------------  */
//  Starting with MSF dataset

import sas using "$crsp/m_stock/msf.sas7bdat", clear
	br
	rename *, lower
	keep permno date ret
	format date %tdCCYY-NN-DD
	gen date_fd = dofm(mofd(date))
	format date_fd %tdCCYY-NN-DD
	gen _year = year(date)
	by permno _year, sort: egen ret_cnt = count(ret)
	drop _year

/*  ---------------------------  */
joinby date_fd using "$temp112/factors_monthly.dta", unmatched(both)
	xtset permno date
	tab _merge
	tab date if _merge ==1
	drop if missing(rf)
	drop _merge

/*  ---------------------------  */
joinby permno date_fd using "$temp112/msedelist.dta", unmatched(both)
	tab _merge
	tab _merge, nolabel
	drop if _merge==2
	drop _merge

	rename ret ret1
	gen ret = ((1+ret1) * (1+dlret)) -1
		replace ret = dlret if missing(ret) & missing(ret1)
		replace ret = ret1 if missing(ret) & missing(dlret)
	drop ret1 dlstdt dlret
	drop if missing(ret)
	gen exret = ret - (mktrf+rf)

/*  ---------------------------  */

	xtset permno date
	by permno: gen date_n = _n

save "$temp112/monthlyrets.dta", replace

use "$temp112/monthlyrets.dta", clear

// 60-month moving standard deviation of ret/exret
rangestat (count) ret_movsum=exret (sd) ret_std=ret exret_std=exret, ///
	interval(date_n -59 0) by(permno) 

// 24-month moving standard deviation of ret/exret
rangestat (count) ret_movsum24=ret (sd) ret_std24=ret exret_std24=exret,  ///
	interval(date_n -23 0) by(permno) 

//  12-month moving exret
rangestat (count) _count=ret (sum) exret_12m=ret, ///
	interval(date_n -11 0) by(permno) 
drop _count

drop mktrf rf mkt ret exret ret_movsum date_fd

save "$temp112/returns.dta", replace

/*  ---------------------------  */
use "$temp112/monthlyrets.dta", clear

gen month = month(date)
gen year = year(date)
keep permno date year month exret ret_cnt
order permno date year month exret ret_cnt

gen month1_2 = 0
	replace month1_2 = 1 if month == 1 | month == 2
bysort permno year: egen xret_202002 = sum(exret) if month1_2==1
drop month1_2

gen month3_4 = 0
	replace month3_4 = 1 if month == 3 | month == 4
bysort permno year: egen xret_202004 = sum(exret) if month3_4==1
drop month3_4

gen month8_12 = 0
	replace month8_12 = 1 if inrange(month,5,12)
bysort permno year: egen xret_202012 = sum(exret) if month8_12
drop month8_12

foreach var of varlist xret_202002 xret_202004 xret_202012 {
bysort permno year: egen e`var' = max(`var')
drop `var'
}

sort permno date
keep permno year ret_cnt exret_202002 exret_202004 exret_202012
duplicates drop *, force

save "$temp112/returns2.dta", replace


/*  ------------------------------------------------------------------------  */
//  Firm age

import sas using "$crsp/m_stock/dsenames.sas7bdat", clear
	br
	rename *, lower
	keep permno permco namedt
	sort permco namedt
	by permco: egen namedt_start = min(namedt)
	format namedt namedt_start %tdCCYY-NN-DD
	keep permno namedt_start
	duplicates drop *, force
save "$temp112/firmage.dta", replace

/*  ------------------------------------------------------------------------  */
//  Get Jan-Feb high and March-April low

import sas using "$crsp/m_stock/dsf.sas7bdat", clear
	br
	rename *, lower
	format date %tdCCYY-NN-DD
	keep if inrange(month(date),1,4)

	keep permno date bidlo askhi cfacpr
	gen askhi_adj = abs(askhi) * cfacpr
		replace askhi_adj = . if !inrange(month(date),1,2)
	gen bidlo_adj = abs(bidlo) * cfacpr
		replace bidlo_adj = . if !inrange(month(date),3,4)
	gen year = year(date)
	drop bidlo askhi cfacpr

	sort permno year date
	by permno year: egen _high_price = max(askhi_adj)
	by permno year: egen _low_price = min(bidlo_adj)
	gen prc_delta = ((_low_price / _high_price)-1)
	keep permno year prc_delta _low_price

	duplicates drop *, force

save "$temp112/highlow.dta", replace


/*  -|----------------------------------------------------------------------  */
/*  ||  ---    Program end: a01-04-crsp.do                                    */
/*  -|  ---    Attila Balogh    --------------------------------------------  */